//
//  MainTabBarController.m
//  weibo-01
//
//  Created by HZApple on 16/5/9.
//  Copyright © 2016年 hdu. All rights reserved.
//

#import "MainTabBarController.h"
#import "ThemeButton.h"
#import "ThemeImageView.h"

@interface MainTabBarController ()

@property (strong, nonatomic) ThemeImageView *selectImage;

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self _loadSubviewControllers];
    
    
    [self _createCustomTabBar];
}

- (void)_loadSubviewControllers{
    
    NSArray *storyboardNames = @[@"Home",@"Message",@"Profile",@"Discover",@"More"];
    
    NSMutableArray *navs = [NSMutableArray array];
    //通过storyboard获取navigationcontroller
    for (NSString * str in storyboardNames) {
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:str bundle:nil];
        
        UINavigationController *nav = [storyboard instantiateInitialViewController];
        
        [navs addObject:nav];
        
    }
    
    self.viewControllers = navs;
    
}
- (void)_createCustomTabBar {
    
    for (UIView *view in self.tabBar.subviews) {
        
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [view removeFromSuperview];
            
        }
        
    }
    
    CGFloat width = kScreenWidth / 5;

    for (NSInteger i = 0; i < 5; i++) {
        
        
        ThemeButton *button = [[ThemeButton alloc] initWithFrame:CGRectMake(i * width, 0, width, kTabbarHeight)];;

        
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        button.tag = i + 10;
        
        
        
        NSString *imageName = [NSString stringWithFormat:@"home_tab_icon_%ld.png", i + 1];
        
        button.normalImgName = imageName;
        
        [self.tabBar addSubview:button];
        
        
    }
    
    //插入一个图片图层，使得效果上达到换标签栏背景
    ThemeImageView *imageView = [[ThemeImageView alloc] initWithFrame:self.tabBar.bounds];
    NSLog(@"%@",NSStringFromCGRect(self.tabBar.bounds));
    [imageView setImageName:@"mask_navbar.png"];
    [self.tabBar insertSubview:imageView atIndex:0];


    
    
    
    //选中图片
    _selectImage = [[ThemeImageView alloc] initWithFrame:CGRectMake(0, 0, width, kTabbarHeight)];
    _selectImage.imageName = @"home_bottom_tab_arrow.png";
 
    [self.tabBar insertSubview:_selectImage atIndex:1];
    
    
}

- (void)clickButton:(UIButton *)button {
    
    self.selectedIndex = button.tag - 10;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        _selectImage.center = button.center;
        
    }];
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
